package main

import (
	"container/list"
	"fmt"
	"sync"
)

const N int = 10

type QueueNode struct {
	figure  int
	digits1 [N]int
	digits2 [N]int
	sflag   bool

	data *list.List
}

var lock sync.Mutex

func newQueue() *QueueNode {
	q := new(QueueNode)
	q.data = list.New()
	return q
}

func (q *QueueNode) push(v interface{}) {
	defer lock.Unlock()
	lock.Lock()
	q.data.PushFront(v)
}

func (q *QueueNode) dump() {
	for iter := q.data.Back(); iter != nil; iter = iter.Prev() {
		fmt.Println("item:", iter.Value)
	}
}

func (q *QueueNode) pop() interface{} {
	defer lock.Unlock()
	lock.Lock()
	iter := q.data.Back()
	v := iter.Value
	q.data.Remove(iter)
	return v
}

func main() {
	var n QueueNode

	n.figure = 1
	n.digits1[0] = 1
	n.digits2[0] = 1
	n.sflag = true

	n2 := n
	n2.digits1[n2.figure] = 2
	n2.digits2[n2.figure] = 2
	n2.figure++
	n2.sflag = false

	n3 := n2
	n3.digits1[n2.figure] = 3
	n3.digits2[n2.figure] = 4
	n3.figure++

	q := newQueue()
	q.push(n)
	q.push(n2)
	q.push(n3)

	q.dump()

	fmt.Printf("\nlen=%d\n\n", q.data.Len())

	for q.data.Len() > 0 {
		x := q.pop().(QueueNode)
		output_node(&x)
	}

}

func output_node(n *QueueNode) {
	fmt.Printf("Figure =%d\n", n.figure)

	fmt.Printf("Array1: ")
	for i := n.figure - 1; i >= 0; i-- {
		fmt.Printf("%d ", n.digits1[i])
	}
	fmt.Println("")

	fmt.Printf("Array2: ")
	for i := n.figure - 1; i >= 0; i-- {
		fmt.Printf("%d ", n.digits2[i])
	}
	fmt.Println("")

	if n.sflag {
		fmt.Printf("SFlag=true\n")
	} else {
		fmt.Printf("SFlag=false\n")
	}

	fmt.Println("")
}